#include "stdafx.h"
#include "wf_dimdiametric.h"



WF_DimDiametric::WF_DimDiametric(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :WF_Dimension("Draw Diametric Dimensions",
                    container, graphicView) {
    reset();
}


QAction* WF_DimDiametric::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Diametric")
    QAction* action = new QAction(tr("&Diametric"),  NULL);
	action->setIcon(QIcon(":/extui/dimdiametric.png"));
    //action->zetStatusTip(tr("Diametric Dimension"));
    return action;
}

void WF_DimDiametric::reset() {
    WF_Dimension::reset();

    edata = PF_DimDiametricAttribute(PF_Vector(false),
                                0.0);
    entity = NULL;
    pos = PF_Vector(false);
    PF_DIALOGFACTORY->requestOptions(this, true, true);
}



void WF_DimDiametric::trigger() {
    PF_PreviewActionInterface::trigger();

    preparePreview();
    if (entity!=NULL) {
        PF_DimDiametric* newEntity = NULL;

        newEntity = new PF_DimDiametric(container,
                                        data,
                                        edata);

        newEntity->setLayerToActive();
        newEntity->setPenToActive();
        newEntity->update();
        container->addEntity(newEntity);

        // upd. undo list:
        if (document!=NULL) {
            document->startUndoCycle();
            document->addUndoable(newEntity);
            document->endUndoCycle();
        }
        PF_Vector rz = graphicView->getRelativeZero();
		graphicView->redraw(LX_Define::RedrawDrawing);
        graphicView->moveRelativeZero(rz);

    } else {
        PF_DEBUG->print("PF_ActionDimDiametric::trigger:"
                        " Entity is NULL\n");
    }
}



void WF_DimDiametric::preparePreview() {
    if (entity!=NULL) {
        double radius=0.0;
        PF_Vector center = PF_Vector(false);
        if (entity->rtti()==LX_Define::EntityArc) {
            radius = ((PF_Arc*)entity)->getRadius();
            center = ((PF_Arc*)entity)->getCenter();
        } else if (entity->rtti()==LX_Define::EntityCircle) {
            radius = ((PF_Circle*)entity)->getRadius();
            center = ((PF_Circle*)entity)->getCenter();
        }
        double angle = center.angleTo(pos);

        data.definitionPoint.setPolar(radius, angle+M_PI);
        data.definitionPoint += center;

        edata.definitionPoint.setPolar(radius, angle);
        edata.definitionPoint += center;
    }
}



void WF_DimDiametric::mouseMoveEvent(QMouseEvent* e) {
    PF_DEBUG->print("PF_ActionDimDiametric::mouseMoveEvent begin");

    PF_Vector mouse(graphicView->toGraphX(e->x()),
                    graphicView->toGraphY(e->y()));

    switch (getStatus()) {
    case SetEntity:
        entity = catchEntity(e, LX_Define::ResolveAll);
        break;

    case SetPos:
        if (entity!=NULL) {
            pos = snapPoint(e);

            preparePreview();
            PF_DimDiametric* d = new PF_DimDiametric(preview, data, edata);
            d->update();

            deletePreview();
            preview->addEntity(d);
            drawPreview();
        }
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionDimDiametric::mouseMoveEvent end");
}



void WF_DimDiametric::mouseReleaseEvent(QMouseEvent* e) {

    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {
        case SetEntity: {
                PF_Entity* en = catchEntity(e, LX_Define::ResolveAll);
                if (en!=NULL) {
                    if (en->rtti()==LX_Define::EntityArc ||
                            en->rtti()==LX_Define::EntityCircle) {

                        entity = en;
                        PF_Vector center;
                        if (entity->rtti()==LX_Define::EntityArc) {
                            center =
                                ((PF_Arc*)entity)->getCenter();
                        } else if (entity->rtti()==LX_Define::EntityCircle) {
                            center =
                                ((PF_Circle*)entity)->getCenter();
                        }
                        graphicView->moveRelativeZero(center);
                        setStatus(SetPos);
                    } else {
                        PF_DIALOGFACTORY->commandMessage(tr("Not a circle "
                                                            "or arc entity"));
                    }
                }
            }
            break;

        case SetPos: {
                PF_CoordinateEvent ce(snapPoint(e));
                coordinateEvent(&ce);
            }
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }

}



void WF_DimDiametric::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    switch (getStatus()) {
    case SetPos:
        pos = e->getCoordinate();
        trigger();
        reset();
        setStatus(SetEntity);
        break;

    default:
        break;
    }
}



void WF_DimDiametric::commandEvent(PF_CommandEvent* e) {
    QString c = e->getCommand().toLower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }

    // setting new text label:
    if (getStatus()==SetText) {
        setText(c);
        PF_DIALOGFACTORY->requestOptions(this, true, true);
        graphicView->enableCoordinateInput();
        setStatus(lastStatus);
        return;
    }

    // command: text
    if (checkCommand("text", c)) {
        lastStatus = (Status)getStatus();
        graphicView->disableCoordinateInput();
        setStatus(SetText);
    }

    // setting angle
    if (getStatus()==SetPos) {
        bool ok;
        double a = PF_Math::eval(c, &ok);
        if (ok==true) {
            pos.setPolar(1.0, PF_Math::deg2rad(a));
            pos += data.definitionPoint;
            trigger();
            reset();
            setStatus(SetEntity);
        } else {
            PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
        }
        return;
    }
}



QStringList WF_DimDiametric::getAvailableCommands() {
    QStringList cmd;

    switch (getStatus()) {
    case SetEntity:
    case SetPos:
        cmd += command("text");
        break;

    default:
        break;
    }

    return cmd;
}


void WF_DimDiametric::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetEntity:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Select arc or circle entity"),
                                            tr("Cancel"));
        break;
    case SetPos:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify dimension line location"), tr("Cancel"));
        break;
    case SetText:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_DimDiametric::showOptions() {
    PF_ActionInterface::showOptions();

    PF_DIALOGFACTORY->requestOptions(this, true);
}



void WF_DimDiametric::hideOptions() {
    PF_ActionInterface::hideOptions();

    //PF_DIALOGFACTORY->requestDimDiametricOptions(edata, false);
    PF_DIALOGFACTORY->requestOptions(this, false);
}



// EOF
